/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.apresentacao.dialog.backup;

import backup.Backup;
import camada.bancodedados.daoimplemente.backup.BackupDAOImpl;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.util.logging.Level;
import javax.swing.JFileChooser;
import org.jdesktop.swingx.error.ErrorInfo;
import utilitarios.erros.MessagemErro;

/**
 *
 * @author GEMADA
 */
public class InternalFrameBackup extends javax.swing.JInternalFrame {

    BackupDAOImpl daoBackup = new BackupDAOImpl();

    /**
     * Creates new form InternalFrameBackup
     */
    public InternalFrameBackup() {
        initComponents();
        this.setTitle("Backup Banco de Dados");
        carregarBackup();

        //Image im = Toolkit.getDefaultToolkit().getImage("./src/ima/sistema/Icone_Sistema.png"); 
        //this.setIconImage(im);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXTitledPanel1 = new org.jdesktop.swingx.JXTitledPanel();
        jLabel1 = new javax.swing.JLabel();
        txtPath = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtDiretorioBanco = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtBanco = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtSenha = new javax.swing.JPasswordField();
        btSeletor = new org.jdesktop.swingx.JXHyperlink();
        btSeletorBanco = new org.jdesktop.swingx.JXHyperlink();
        fileChooser1 = new javax.swing.JFileChooser();
        jXTaskPaneContainer1 = new org.jdesktop.swingx.JXTaskPaneContainer();
        jXTaskPane1 = new org.jdesktop.swingx.JXTaskPane();
        btSalvar = new org.jdesktop.swingx.JXHyperlink();
        btEditar = new org.jdesktop.swingx.JXHyperlink();
        btCancelar = new org.jdesktop.swingx.JXHyperlink();
        jXTaskPane2 = new org.jdesktop.swingx.JXTaskPane();
        btExecutar = new org.jdesktop.swingx.JXHyperlink();

        javax.swing.GroupLayout jXTitledPanel1Layout = new javax.swing.GroupLayout(jXTitledPanel1.getContentContainer());
        jXTitledPanel1.getContentContainer().setLayout(jXTitledPanel1Layout);
        jXTitledPanel1Layout.setHorizontalGroup(
            jXTitledPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jXTitledPanel1Layout.setVerticalGroup(
            jXTitledPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        setBorder(null);
        setClosable(true);
        setTitle("Backup");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/sistema/Icone_Sistema.png"))); // NOI18N

        jLabel1.setText("MYSQL PATH:");

        txtPath.setText("C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin");
        txtPath.setEnabled(false);
        txtPath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPathActionPerformed(evt);
            }
        });

        jLabel5.setText("Diretório banco:");

        txtDiretorioBanco.setText("C:\\Backup");
        txtDiretorioBanco.setEnabled(false);

        jLabel4.setText("Banco de Dados:");

        txtBanco.setText("eloma");
        txtBanco.setEnabled(false);
        txtBanco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBancoActionPerformed(evt);
            }
        });

        jLabel2.setText("Usuario:");

        txtUsuario.setText("root");
        txtUsuario.setEnabled(false);

        jLabel3.setText("Senha:");

        txtSenha.setText("i122825");
        txtSenha.setEnabled(false);

        btSeletor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/Seletor_Arquivos.png"))); // NOI18N
        btSeletor.setText("");
        btSeletor.setEnabled(false);
        btSeletor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSeletorActionPerformed(evt);
            }
        });

        btSeletorBanco.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/Seletor_Arquivos.png"))); // NOI18N
        btSeletorBanco.setText("");
        btSeletorBanco.setEnabled(false);
        btSeletorBanco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSeletorBancoActionPerformed(evt);
            }
        });

        jXTaskPaneContainer1.setBackground(new java.awt.Color(0, 153, 153));
        jXTaskPaneContainer1.setMinimumSize(new java.awt.Dimension(164, 452));
        jXTaskPaneContainer1.setPreferredSize(new java.awt.Dimension(164, 452));

        jXTaskPane1.setTitle("Operações Basicas");

        btSalvar.setForeground(new java.awt.Color(0, 0, 0));
        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/pequenos/Salvar_16x16.png"))); // NOI18N
        btSalvar.setText("Salvar");
        btSalvar.setClickedColor(new java.awt.Color(0, 0, 0));
        btSalvar.setEnabled(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        jXTaskPane1.getContentPane().add(btSalvar);

        btEditar.setForeground(new java.awt.Color(0, 0, 0));
        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/pequenos/Editar_16x16.png"))); // NOI18N
        btEditar.setText("Editar");
        btEditar.setClickedColor(new java.awt.Color(0, 0, 0));
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });
        jXTaskPane1.getContentPane().add(btEditar);

        btCancelar.setForeground(new java.awt.Color(0, 0, 0));
        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/pequenos/Cancelar_16x16.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.setClickedColor(new java.awt.Color(0, 0, 0));
        btCancelar.setEnabled(false);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });
        jXTaskPane1.getContentPane().add(btCancelar);

        jXTaskPaneContainer1.add(jXTaskPane1);

        jXTaskPane2.setTitle("Backup");

        btExecutar.setForeground(new java.awt.Color(0, 0, 0));
        btExecutar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/pequenos/bt_Executar.png"))); // NOI18N
        btExecutar.setText("Executar");
        btExecutar.setClickedColor(new java.awt.Color(0, 0, 0));
        btExecutar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExecutarActionPerformed(evt);
            }
        });
        jXTaskPane2.getContentPane().add(btExecutar);

        jXTaskPaneContainer1.add(jXTaskPane2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtSenha, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtUsuario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4)
                    .addComponent(txtBanco, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtDiretorioBanco, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPath, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btSeletor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btSeletorBanco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(138, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(99, Short.MAX_VALUE)
                    .addComponent(fileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(695, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPath, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btSeletor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btSeletorBanco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDiretorioBanco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtBanco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(3, 3, 3)
                .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(481, Short.MAX_VALUE)
                    .addComponent(fileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(109, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPathActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPathActionPerformed

    private void btSeletorBancoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSeletorBancoActionPerformed
        try {
            fileChooser1.setCurrentDirectory(new File("."));
            fileChooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int result = fileChooser1.showOpenDialog(this);

            if (result == JFileChooser.APPROVE_OPTION) {
                txtPath.setText(fileChooser1.getSelectedFile().getPath());
            }

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }//GEN-LAST:event_btSeletorBancoActionPerformed

    private void btExecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExecutarActionPerformed
        Backup bk = new Backup(txtUsuario.getText(), txtSenha.getText(), txtDiretorioBanco.getText(), txtBanco.getText(), txtPath.getText());
    }//GEN-LAST:event_btExecutarActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        btSalvar.setEnabled(true);
        btEditar.setEnabled(false);
        btCancelar.setEnabled(true);

        txtDiretorioBanco.setEnabled(true);
        txtBanco.setEnabled(true);
        txtUsuario.setEnabled(true);
        txtSenha.setEnabled(true);

    }//GEN-LAST:event_btEditarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        btSalvar.setEnabled(false);
        btEditar.setEnabled(true);
        btCancelar.setEnabled(false);

        txtDiretorioBanco.setEnabled(false);
        txtBanco.setEnabled(false);
        txtUsuario.setEnabled(false);
        txtSenha.setEnabled(false);
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed

        txtDiretorioBanco.setEnabled(false);
        txtBanco.setEnabled(false);
        txtUsuario.setEnabled(false);
        txtSenha.setEnabled(false);

        eloma.classe.backup.Backup backup = new eloma.classe.backup.Backup();

        backup.setPath(txtPath.getText());
        backup.setCaminho(txtDiretorioBanco.getText());
        backup.setBanco(txtBanco.getText());
        backup.setUser(txtUsuario.getText());
        backup.setPassword(txtSenha.getText());

        daoBackup.atualizarBackup(backup);

    }//GEN-LAST:event_btSalvarActionPerformed

    private void txtBancoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBancoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtBancoActionPerformed

    private void btSeletorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSeletorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btSeletorActionPerformed

    public eloma.classe.backup.Backup carregarBackup() {

        eloma.classe.backup.Backup backup = new eloma.classe.backup.Backup();

        try {

            backup = daoBackup.pesquisaBackup();

            txtPath.setText(backup.getPath());
            txtDiretorioBanco.setText(backup.getCaminho());
            txtBanco.setText(backup.getBanco());
            txtUsuario.setText(backup.getUser());
            txtSenha.setText(backup.getPassword());

        } catch (Exception ex) {

        }

        return backup;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXHyperlink btCancelar;
    private org.jdesktop.swingx.JXHyperlink btEditar;
    private org.jdesktop.swingx.JXHyperlink btExecutar;
    private org.jdesktop.swingx.JXHyperlink btSalvar;
    private org.jdesktop.swingx.JXHyperlink btSeletor;
    private org.jdesktop.swingx.JXHyperlink btSeletorBanco;
    private javax.swing.JFileChooser fileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private org.jdesktop.swingx.JXTaskPane jXTaskPane1;
    private org.jdesktop.swingx.JXTaskPane jXTaskPane2;
    private org.jdesktop.swingx.JXTaskPaneContainer jXTaskPaneContainer1;
    private org.jdesktop.swingx.JXTitledPanel jXTitledPanel1;
    private javax.swing.JTextField txtBanco;
    private javax.swing.JTextField txtDiretorioBanco;
    private javax.swing.JTextField txtPath;
    private javax.swing.JPasswordField txtSenha;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
